import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:study_demo/base/BaseController.dart';

import '../util/utils.dart';

mixin JsonTransformModelController on BaseController {
  final model = ''.obs;

  TextEditingController _resultTextEditingController = TextEditingController();

  TextEditingController get resultTextEditingController =>
      _resultTextEditingController;

  TextEditingController _classNameTextEditingController =
      TextEditingController();

  TextEditingController get classNameTextEditingController =>
      _classNameTextEditingController;

  TextEditingController _modelTextEditingController = TextEditingController();

  TextEditingController get modelTextEditingController =>
      _modelTextEditingController;

  transform() {
    String attributeInfo = '';
    String jsonData = _resultTextEditingController.text;
    if (isValidJson(jsonData)) {
      print('jsonData:$jsonData');
      Object jsonArray = jsonDecode(jsonData);
      Map<String, dynamic>? jsonObject;
      if (jsonArray is List) {
        if (jsonArray.isNotEmpty) {
          jsonObject = jsonArray.first;
        }
      } else {
        if (jsonArray is String)
          jsonObject = jsonArray as Map<String, dynamic>?;
      }
      // 遍历Map获取所有键值对
      jsonObject?.forEach((key, value) {
        print('Key: $key');
        String valueType;
        if (value is int)
          valueType = 'int?';
        else if (value is double)
          valueType = 'double?';
        else if (value is String)
          valueType = 'String?';
        else if (value is bool)
          valueType = 'bool?';
        else if (value is List)
          valueType = 'List?';
        else if (value is Map)
          valueType = 'Map?';
        else
          valueType = 'Unknown';
        if (key.contains('_')) {}
        attributeInfo += (key.contains('_') ? '@JsonKey(name:"$key")\n' : '') +
            valueType +
            ' ' +
            (key.contains('_') ? key.replaceAll('_', '') : key) +
            ';\n';
      });
      if (!_classNameTextEditingController.text.isAlphabetOnly) {
        toast('类名格式不正确');
        return;
      }
      String className = firstWordToUpper(_classNameTextEditingController.text);
      print('className:$className');
      model.value =
          '@JsonSerializable()\nclass $className extends Object {\n $attributeInfo\n$className();\n\n' +
              'factory $className.fromJson(Map<String, dynamic> srcJson) =>'
                  '\_\$$className\FromJson(srcJson);\n\nMap<String?, dynamic> toJson() => _\$$className\ToJson(this);\n}';
      _modelTextEditingController.text = model.value;
      print(model.value);
    } else {
      toast('当前内容不是json格式');
    }
  }

  clearResult() {
    resultTextEditingController.clear();
  }

  clearModel() {
    _modelTextEditingController.clear();
  }

  copyResult() {
    copyToClipboard(_resultTextEditingController.text);
  }

  copyAll() {
    copyToClipboard(_modelTextEditingController.text);
  }
}
